/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
// import image from '@ohos.multimedia.image';


export function getPixelMapInt32ArrayData(pixelMap) {
    return new Promise((resolve, reject) => {
        // 读取新创建的PixelMap
        let pixelBytesNumber = pixelMap.getPixelBytesNumber();
        print(TAG + 'getPixelMapInt32ArrayData pixelBytesNumber: ' + pixelBytesNumber);
        let readPixelsToBuffer = new ArrayBuffer(pixelBytesNumber);
        let readPixelsToUnit8Arr = new Int32Array(readPixelsToBuffer);
        pixelMap.readPixelsToBuffer(readPixelsToBuffer, (InfoErr, InfoData) => {
            if (InfoErr) {
                print(TAG + 'getPixelMapInt32ArrayData error code is  ' + InfoErr.code);
                print(TAG + 'getPixelMapInt32ArrayData error msg is ' + InfoErr.message);
            }
            else {
                print(TAG + 'getPixelMapInt32ArrayData success');
                print(TAG + 'getPixelMapInt32ArrayData readPixelsToUnit8Arr size:' + readPixelsToUnit8Arr.length);
                print(TAG + 'getPixelMapInt32ArrayData readPixelsToUnit8Arr:' + JSON.stringify(readPixelsToUnit8Arr));
                resolve(readPixelsToUnit8Arr);
            }
        });
    });
}
export function getMatrixPixelData(matrix, width, height) {
    const BLACK = 0xFF000000;
    const WHITE = 0xFFFFFFFF;
    const pixels = new Uint32Array(width * height);
    for (let y = 0; y < height; y++) {
        let offset = y * width;
        for (let x = 0; x < width; x++) {
            pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
        }
    }
    return pixels;
}
let TAG = "imageUtils-----------------------";
